//
//  YiYuanPeiHuViewController.m
//  XiangMu
//
//  Created by  方崇豪 on 15/7/13.
//  Copyright (c) 2015年  方崇豪. All rights reserved.
//

#define BIAO_X peiHuleixing.frame.origin.x+peiHuleixing.frame.size.width
#define BIAO_Y peiHuleixing.frame.origin.y+peiHuleixing.frame.size.height
#define BIAO_WIDH shiJianBtn.frame.size.width
#define BIAO_HIGHT shiJianLable.frame.size.height

#import "YiYuanPeiHuViewController.h"


@implementation ASButton

@synthesize index;

@end

@interface YiYuanPeiHuViewController ()

@end

@implementation YiYuanPeiHuViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.navigationController.navigationBarHidden=NO;
    tableview.hidden=YES;
//   arrChanFang=[[NSMutableArray alloc]initWithObjects:@"产房陪护",@"外伤陪护",@"重症陪护",@"其他陪护", nil];
//    self.tabBarController.tabBar.hidden=YES;
    [self KongJian];
  
}
-(void)KongJian{
    //返回
    UIButton*fanHuiBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    fanHuiBtn.frame=CGRectMake(20, 20, 10, 20);
//    fanHuiBtn.backgroundColor=[UIColor redColor];
    [fanHuiBtn addTarget:self action:@selector(FanHui) forControlEvents:UIControlEventTouchUpInside];
    [fanHuiBtn setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    UIBarButtonItem*bar=[[UIBarButtonItem alloc]initWithCustomView:fanHuiBtn];
    self.navigationItem.leftBarButtonItem=bar;
    self.title=self.tittexte;
//    [self.view addSubview:fanHuiBtn];

    //跳过
    UIButton*tiaoGuoBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    tiaoGuoBtn.frame=CGRectMake(kScreenW/2+120, 0, 30,30);
    [tiaoGuoBtn addTarget:self action:@selector(TiaoGuo) forControlEvents:UIControlEventTouchUpInside];
 
    [tiaoGuoBtn setTitle:@"跳过" forState:UIControlStateNormal];
    [tiaoGuoBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [tiaoGuoBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    
    UIBarButtonItem*barbutton=[[UIBarButtonItem alloc]initWithCustomView:tiaoGuoBtn];
    self.navigationItem.rightBarButtonItem=barbutton;
    
//    [self.view addSubview:tiaoGuoBtn];
    //陪护类型
    
    peiHuleixing=[[UILabel alloc]initWithFrame:CGRectMake(kScreenW/2-150, fanHuiBtn.frame.origin.y+40, 65, 30)];
    peiHuleixing.text=@"陪护类型:";
    [peiHuleixing setFont:[UIFont systemFontOfSize:15]];
    [self.view addSubview:peiHuleixing];
    
    leiXingBtn=[ASButton buttonWithType:UIButtonTypeCustom];
    [leiXingBtn addTarget:self action:@selector(ShiJian:) forControlEvents:UIControlEventTouchUpInside];
    
    leiXingBtn.frame=CGRectMake(peiHuleixing.frame.origin.x+peiHuleixing.frame.size.width, peiHuleixing.frame.origin.y,peiHuleixing.frame.size.width+160, peiHuleixing.frame.size.height) ;
    leiXingBtn.tag=1000;
    [leiXingBtn.layer setMasksToBounds:YES];
    
    [leiXingBtn.layer setBorderWidth:0.5];
    [leiXingBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    leiXingBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    leiXingBtn.contentEdgeInsets=UIEdgeInsetsMake(0, 10, 0, 0);
    
//    [leiXingBtn setTitle:@"陪护" forState:UIControlStateNormal];
    [leiXingBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.view addSubview:leiXingBtn];
    UIImageView * xuanXiangImagView=[[UIImageView alloc]initWithFrame:CGRectMake( leiXingBtn.frame.origin.x+leiXingBtn.frame.size.width-20,leiXingBtn.frame.origin.y+10, 11, 6)];
    xuanXiangImagView.image=[UIImage imageNamed:@"sanjiaoxing"];
    [self.view addSubview:xuanXiangImagView];
    
    
    //个人体重
    UILabel*tiZhongLabel=[[UILabel alloc]initWithFrame:CGRectMake(kScreenW/2-150,peiHuleixing.frame.origin.y+80, 66, 30)];
    [tiZhongLabel setFont:[UIFont systemFontOfSize:15]];

    
    [self.view addSubview:tiZhongLabel];
    
   tiZhongBtn=[ASButton buttonWithType:UIButtonTypeCustom];
    
    tiZhongBtn.frame=CGRectMake(tiZhongLabel.frame.origin.x+tiZhongLabel.frame.size.width, tiZhongLabel.frame.origin.y, tiZhongLabel.frame.size.width+160, tiZhongLabel.frame.size.height) ;
    [tiZhongBtn addTarget:self action:@selector(ShiJian:) forControlEvents:UIControlEventTouchUpInside];
    [tiZhongBtn.layer setMasksToBounds:YES];
    
    tiZhongBtn.tag=1001;
    
    [tiZhongBtn.layer setBorderWidth:0.5];
    
    [tiZhongBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];

    tiZhongBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    tiZhongBtn.contentEdgeInsets=UIEdgeInsetsMake(0, 10, 0, 0);
   
    
 
//    self.view.backgroundColor=UIColorFromRGB(0X00cc85);
    
    
    [tiZhongBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    if (_isLaoRen) {
        
        tiZhongLabel.text=@"性    别";
        

        
    }
    else
    {

        tiZhongLabel.text=@"个人体重:";
    }

    [self.view addSubview:tiZhongBtn];
    
    UIImageView * tiZhongImagView=[[UIImageView alloc]initWithFrame:CGRectMake( tiZhongBtn.frame.origin.x+tiZhongBtn.frame.size.width-20,tiZhongBtn.frame.origin.y+10, 11, 6)];
    tiZhongImagView.image=[UIImage imageNamed:@"sanjiaoxing"];
    [self.view addSubview:tiZhongImagView];
    
    //陪护时间
    shiJianLable= [[UILabel alloc]initWithFrame:CGRectMake(kScreenW/2-150,tiZhongLabel.frame.origin.y+80, 65, 30)];
    
    [shiJianLable setFont:[UIFont systemFontOfSize:15]];
    shiJianLable.text=@"陪护时间:";
    
    [self.view addSubview:shiJianLable];
    
    shiJianBtn = [ASButton buttonWithType:UIButtonTypeCustom];
    
    shiJianBtn.frame=CGRectMake(shiJianLable.frame.origin.x+shiJianLable.frame.size.width, shiJianLable.frame.origin.y, shiJianLable.frame.size.width+160, shiJianLable.frame.size.height) ;
    [shiJianBtn addTarget:self action:@selector(ShiJian:) forControlEvents:UIControlEventTouchUpInside];
    [shiJianBtn.layer setMasksToBounds:YES];
    shiJianBtn.tag=1002;
    [shiJianBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [shiJianBtn.layer setBorderWidth:0.5];
    
    shiJianBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    shiJianBtn.contentEdgeInsets=UIEdgeInsetsMake(0, 10, 0, 0);
    
    
//    [shiJianBtn setTitle:@"陪护" forState:UIControlStateNormal];
    [shiJianBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.view addSubview:shiJianBtn];
    UIImageView * ShiJianImagView=[[UIImageView alloc]initWithFrame:CGRectMake( shiJianBtn.frame.origin.x+shiJianBtn.frame.size.width-20,shiJianBtn.frame.origin.y+10, 11, 6)];
    ShiJianImagView.image=[UIImage imageNamed:@"sanjiaoxing"];
    
    [self.view addSubview:ShiJianImagView];
    //表
    tableview=[[UITableView alloc]initWithFrame:CGRectMake(BIAO_X,BIAO_Y, shiJianBtn.frame.size.width, 0) style:UITableViewStylePlain];
    tableview.backgroundColor=CL_e6e6e6;
    tableview.delegate=self;
    tableview.dataSource=self;
    tableview.hidden=YES;
    //提交
    UIButton*tiJiaoBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    tiJiaoBtn.frame=CGRectMake(40, kScreenH/2+40, kScreenW-80, kScreenW/4-50);
    [tiJiaoBtn addTarget:self action:@selector(TiJiaoBtn) forControlEvents:UIControlEventTouchUpInside];
//      [tiJiaoBtn setBackgroundColor:[UIColor greenColor]];
    [tiJiaoBtn setTitle:@"提交" forState:UIControlStateNormal];
    ;
    [tiJiaoBtn setBackgroundColor:CL_00cc85];
    [tiJiaoBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:tiJiaoBtn];
    //请求数据
    [self YiYuanShuJu];
    
    [self.view addSubview:tableview];

    
    
    [self shouw:@""];
    
}
-(void)shouw:(NSString * )str{
    
    hud=[MBProgressHUD showHUDAddedTo:self.view  animated:YES];
    hud.labelText = @"数据加载中...";
    [hud show:YES];
}
//提示网络消息
-(void)showMessage:(NSString *)message
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    UIView *showview =  [[UIView alloc]init];
    showview.backgroundColor = [UIColor blackColor];
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    CGSize LabelSize = [message sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(210, 9000)];
    label.frame = CGRectMake(0, 0, LabelSize.width, LabelSize.height);
    label.text = message;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:12];
    [showview addSubview:label];
    
    showview.frame = CGRectMake(self.view.bounds.size.height/6, self.view.bounds.size.width, LabelSize.width, LabelSize.height);
    [UIView animateWithDuration:2 animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];
}
//请求的数据
- (void)YiYuanShuJu{
   
    if (!_isLaoRen)
    {
        [QingQiuShuJu YiYuanPeiHu:^(NSMutableArray *arr,BOOL isok)
        {
            if (isok) {
                 _zongDeArr=arr;
                [tableview reloadData];
                [hud hide:YES];
                hud.removeFromSuperViewOnHide=YES;
                
                [self MoRenXianShiDiYiGe];
            }
            else{
                [self.view addSubview:hud];
                hud.mode=MBProgressHUDModeText;
                hud.labelText=@"网络连接超时";
            
                hud.margin = 10.f;
                
                hud.removeFromSuperViewOnHide = YES;
                
                [hud hide:YES afterDelay:1.5];

            }
           
            
        }];
      }
    else{
        
        [QingQiuShuJu SheQuDeLaoRenPeiHu:^(NSMutableArray *arr,BOOL isok)
         {
            
             if (isok)
             {
              
                 _zongDeArr=arr;
                 [tableview reloadData];
                 [hud hide:YES];
                 hud.removeFromSuperViewOnHide=YES;
                [self MoRenXianShiDiYiGe];
             }
             else{
                 [self.view addSubview:hud];
                 hud.mode=MBProgressHUDModeText;
                 hud.labelText=@"网络连接超时";
                 
                 hud.margin = 10.f;
                 
                 hud.removeFromSuperViewOnHide = YES;
                 
                 [hud hide:YES afterDelay:1.5];
                 
             }

        }];
     }
    
}
//默认显示第一个
- (void)MoRenXianShiDiYiGe{
    
    
    
    for (int i =0; i<_zongDeArr.count; i++) {
        arrChanFang = [_zongDeArr objectAtIndex:i];
        YiYuanPeiHuShuJu * y = [arrChanFang objectAtIndex:0];
        
        if (i==0) {
             [leiXingBtn setTitle:y.type forState:UIControlStateNormal];
            leiXingBtn.index = [y.ID integerValue];
        }
        else if (i==1)
        {
              [shiJianBtn setTitle:y.type forState:UIControlStateNormal];
             shiJianBtn.index = [y.ID integerValue];
        }
        else{
           
            [tiZhongBtn setTitle:y.type forState:UIControlStateNormal];
             tiZhongBtn.index = [y.ID integerValue];
        }
       
    }
}
- (void)FanHui
{
    if (!self.isLaoRen) {
        self.tabBarController.tabBar.hidden=NO;
       
    }
 
     [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)ShiJian:(ASButton *)sender {
    buttonSuoYin = sender.tag;
 
    
    CGRect rect;
    rect.origin.x = sender.frame.origin.x;
    rect.origin.y = sender.frame.origin.y+sender.frame.size.height;
    rect.size = sender.frame.size;
    
    
    tableview.frame = rect;
   

    if (!isheed)
    {
         tableview.hidden=NO;
        switch (sender.tag)
        {
            case 1000:
                
            {
                arrChanFang=[_zongDeArr objectAtIndex:0];
                
                [tableview reloadData];
            
            }
                break;
            case 1001:
            {
                if (!_isLaoRen)
                {
                    arrChanFang=[_zongDeArr objectAtIndex:2];
                    
                    [tableview reloadData];
                    
                }else
                {
                    arrChanFang=[_zongDeArr objectAtIndex:2];
                    
                    [tableview reloadData];
                    
                    
                }
                
            }
                break;
            case 1002:
            {
                arrChanFang=[_zongDeArr objectAtIndex:1];
                
                [tableview reloadData];
                
            }
                break;
            default:
                break;
             
                
        }
  tableview.frame=CGRectMake(rect.origin.x, rect.origin.y, BIAO_WIDH, 30*arrChanFang.count);
    }
    else
    {
         tableview.hidden=YES;
    }
    isheed=!isheed;
}
//跳过
- (void)TiaoGuo
{
    PeiHuShiViewController *p=[[PeiHuShiViewController alloc]init];
    p.tabBarController.tabBar.hidden=YES;
    [self.navigationController pushViewController:p animated:YES];
}

//提交
- (void)TiJiaoBtn {
    
    
    PeiHuShiViewController * pei = [[PeiHuShiViewController alloc]init];

    if (!self.isLaoRen)
    {

        
        
        pei.yiyuan = @"医院";
        
        pei.leiXingIndex = leiXingBtn.index ;
        pei.tiZhongIndex = tiZhongBtn.index;
        
        
        
    }
    else{
  
        pei.yiyuan = @"老人";

        pei.leiXingIndex = leiXingBtn.index ;
        pei.tiZhongIndex = tiZhongBtn.index;
    }
    [self.navigationController pushViewController:pei animated:YES];
   
}
#pragma mark--UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return arrChanFang.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString*tab=@"tab";
    
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:tab];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tab];
        UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, BIAO_WIDH, 29)];
        lable.tag = 100;
        lable.textColor = [UIColor blackColor];
        
        lable.font = [UIFont systemFontOfSize:13];
        lable.textAlignment = NSTextAlignmentCenter;
        
        [cell.contentView addSubview:lable];
        

    }
    
    UILabel * lable= (UILabel *)[cell.contentView viewWithTag:100];

    
    YiYuanPeiHuShuJu*y =[arrChanFang objectAtIndex:indexPath.row];
    lable.text=y.type;


    if (indexPath.row%2==0) {
        cell.backgroundColor=CL_e6e6e6;
        
        
    }
    else{
          cell.backgroundColor=CL_e6e6e6;
    }
    
    if (indexPath.row==0) {
        cell.backgroundColor= CL_00cc85;
        cell.textLabel.textColor=[UIColor whiteColor];
    }else{
        cell.textLabel.textColor=[UIColor blackColor];
    }
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell*cell=[tableview cellForRowAtIndexPath:indexPath];
    
    UILabel * lable = (UILabel *) [cell.contentView viewWithTag:100];
    
    YiYuanPeiHuShuJu * y =[arrChanFang objectAtIndex:indexPath.row];
    
    isheed=NO;
    
    switch (buttonSuoYin) {
        case 1000:
        {
            
         leiXingBtn.index = [y.ID integerValue];
         [leiXingBtn setTitle:lable.text forState:UIControlStateNormal];
        }
            break;
        case 1001:
        {
            if (!_isLaoRen) {
                [tiZhongBtn setTitle:[NSString stringWithFormat:@"%@",lable.text]forState:UIControlStateNormal];
                 tiZhongBtn.index = [y.ID integerValue];
            }
            else
            {
            [tiZhongBtn setTitle:[NSString stringWithFormat:@"%@",lable.text]forState:UIControlStateNormal];
                 tiZhongBtn.index = [y.ID integerValue];
            }
        }
            break;
        case 1002:
        {
             [shiJianBtn setTitle:lable.text forState:UIControlStateNormal];
             shiJianBtn.index = [y.ID integerValue];
        }
            break;
        default:
            break;
    }
   
    
    tableview.hidden=YES;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
